'use strict';
const SUN = require('../base/sun')
const idsDao = require('./idsDao')
const db = require('./db')

class PlayerDao {
    constructor() {}

    newAndSave(uId, player, cb) {
        let self = this
        idsDao.findOneAndUpdate('playerId', function (err, numAffected, affectedDocuments) {
            if (err || !affectedDocuments) {
                console.error(err)
            } else {
                let id = affectedDocuments.playerId * 1000 + 1 //id后面3位是服务器id
                let nowTime = new Date().getTime()
                player._id = id
                player.pId = id
                player.createTime = nowTime
                player.loginTime = nowTime
                player.uId = uId
                player.name = player.basicInfo.name
                db.players.insert(player, cb)
            }
        })
    }

    getPlayerByQuery(query, cb) {
        db.players.findOne(query, function (err, doc) {
            cb(err, doc)
        })
    }

    getPlayersByQuery(query, cb) {
        db.players.find(query, function (err, docs) {
            cb(err, docs)
        })
    }

    update(pid, query, cb) {
        db.players.update({
            _id: pid
        }, query, function (err, numReplaced) {
            cb(err)
        })
    }
}

let playerDao = new PlayerDao()
module.exports = playerDao